# -*- Makefile -*- for Dungeon Crawl (dos)
#
# Modified for Crawl Reference by $Author$ on $Date$

# this file contains a list of the libraries.
# it will make a variable called OBJECTS that contains all the libraries
include makefile.obj

# need .exe so make will find the right file
APPNAME = crawl.exe
CXX = gxx
DELETE = del
COPY = copy
OS_TYPE = DOS

# If you don't have flex or bison, set DOYACC to N or empty.
DOYACC := n
LEX  := flex
YACC := bison -y

INSTALLDIR ?= package

ifeq ($(LUASRC),)
LUASRC := util\lua\src
endif

LUALIB = lua
LUALIBA = lib$(LUALIB).a

SQLSRC := util\sqlite
SQLLIB := sql3
SQLLIBA := lib$(SQLLIB).a
FSQLLIBA := $(SQLSRC)\$(SQLLIBA)

PCRESRC := util\pcre
PCRELIB := pcre
PCRELIBA := lib$(PCRELIB).a

LIB = -L$(LUASRC) -l$(LUALIB) -L$(SQLSRC) -l$(SQLLIB) -L$(PCRESRC) -lpcre
INCLUDES := -Iutil -I. -I$(LUASRC) -I$(SQLSRC) -I$(PCRESRC)

WORKDIR := $(shell cd)

CFWARN   := -Wall -Wwrite-strings -Wshadow -pedantic
CFOTHERS := -D$(OS_TYPE) $(EXTRA_FLAGS) -fsigned-char -fstrict-aliasing \
			-DCLUA_BINDINGS

CFLAGS   := $(INCLUDES) $(CFWARN) $(CFOTHERS)
YCFLAGS  := $(INCLUDES) $(CFOTHERS)

OBJECTS += libdos.o crash-d.o

LDFLAGS =

UTIL = util/

# DOS brain damage. What to do, what to do.
YTABC  := levcom~1.c
YTABH  := levcom~1.h

ifeq ($(LEX),)
DOYACC :=
endif

ifeq ($(YACC),)
DOYACC :=
endif

OBJECTS := $(UTIL)levtab.o $(UTIL)levlex.o $(OBJECTS)

GAME_DEPENDS := $(LUASRC)\$(LUALIBA) $(FSQLLIBA) $(PCRESRC)\$(PCRELIBA) $(OBJECTS)

##########################################################################

all:            $(APPNAME)

DEPENDENCY_MKF := makefile.dep
-include $(DEPENDENCY_MKF)

##########################################################################
# The level compiler
#

# [dshaligram] A million plagues on djgpp make! It doesn't want to use a
# generic rule for $(UTIL)%.cc; it always uses the .cc.o: rule instead.

LINC :=

# [ds] If we're using the prebuilt include, we can't copy it around because
# djgpp copy mangles the name irreparably.
ifneq ($(DOYACC),y)
LINC += -I prebuilt
endif

$(UTIL)levlex.o: $(UTIL)levlex.cc
		$(subst /,\,$(CXX) $(LINC) $(YCFLAGS) -o $@ -c $<)

$(UTIL)levtab.o: $(UTIL)levtab.cc
		$(subst /,\,$(CXX) $(LINC) $(YCFLAGS) -o $@ -c $<)

ifeq ($(DOYACC),y)

$(UTIL)levtab.cc: $(UTIL)levcomp.ypp
		$(subst /,\,cd $(UTIL))
		$(YACC) -d -b levcomp levcomp.ypp
		copy $(YTABC) levtab.cc
		cd ..

# djgpp flex must not have a space between -o and its parameter, or no business
# will result.
$(UTIL)levlex.cc: $(UTIL)levcomp.lpp
		$(subst /,\,cd $(UTIL))
		$(LEX) -olevlex.cc levcomp.lpp
		cd ..

else

$(UTIL)levtab.cc: prebuilt/levcom~2.cc
		$(subst /,\,$(COPY) $< $@)

$(UTIL)levlex.cc: prebuilt/levcom~1.cc
		$(subst /,\,$(COPY) $< $@)

endif

##########################################################################

clean:
		$(DELETE) *.o
		$(subst /,\,$(DELETE) $(UTIL)*.o)
		$(subst /,\,$(DELETE) $(UTIL)*.exe)
		$(subst /,\,$(DELETE) $(UTIL)levcom~1.*)
		$(subst /,\,$(DELETE) $(UTIL)levtab.*)
		$(subst /,\,$(DELETE) $(UTIL)levlex.*)

clean-lua:
		cd $(LUASRC)
		$(MAKE) clean_win
		cd $(WORKDIR)

clean-sql:
		cd $(SQLSRC)
		$(MAKE) "RM_F=cmd /c del /f" clean
		cd $(WORKDIR)

clean-pcre:
		cd $(PCRESRC)
		$(MAKE) clean
		cd $(WORKDIR)


distclean: clean-lua clean-sql clean-pcre
		$(DELETE) *.o
		$(DELETE) bones.*
		$(DELETE) morgue.txt
		$(DELETE) scores
		$(DELETE) $(APPNAME)
		$(DELETE) *.sav
		$(DELETE) core
		$(DELETE) *.0*
		$(DELETE) *.lab

$(APPNAME):	$(GAME_DEPENDS)
		${CXX} ${LDFLAGS} $(CFLAGS) $(OBJECTS) -o $(APPNAME) $(LIB)
		strip $(APPNAME)

debug:		$(GAME_DEPENDS)
		${CXX} ${LDFLAGS} $(CFLAGS) $(OBJECTS) -o $(APPNAME) $(LIB)

profile:	$(GAME_DEPENDS)
		${CXX} -g -p ${LDFLAGS} $(CFLAGS) $(OBJECTS) -o $(APPNAME) $(LIB)

.cc.o:
		${CXX} ${CFLAGS} -c $<

# I don't have touch for DOS, but if you do, you can put this back.
#
#.h.cc:
#		touch $@

#############################################################################
# Build Lua

$(LUASRC)\$(LUALIBA):
	@echo Building Lua...
	@cd $(LUASRC)
	@$(MAKE) crawl_dos
	@cd $(WORKDIR)

#############################################################################
# Build SQLite

$(FSQLLIBA):
	@echo Building SQLite
	cd $(SQLSRC)
	$(MAKE) LIBSQL=$(SQLLIBA)
	cd $(WORKDIR)

#############################################################################
# Build PCRE

$(PCRESRC)\$(PCRELIBA):
	@echo Building PCRE...
	@cd $(PCRESRC)
	@$(MAKE) CXX=$(CXX)
	@cd $(WORKDIR)
